<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>自定义Lint | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">自定义Lint</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">June 21, 2016&nbsp;&nbsp;22:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Android-%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/">Android-工具使用</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h3 id="1-为什么要自己写-Lint-规则？"><a href="#1-为什么要自己写-Lint-规则？" class="headerlink" title="1.    为什么要自己写 Lint 规则？"></a>1.    为什么要自己写 Lint 规则？</h3><h4 id="1-如果你在写一个代码库-SDK，你想帮助开发者正确地使用它，Lint-规则就能派上用场。有了-Lint，你可以轻易地提醒他们忽略或做错的事情。"><a href="#1-如果你在写一个代码库-SDK，你想帮助开发者正确地使用它，Lint-规则就能派上用场。有了-Lint，你可以轻易地提醒他们忽略或做错的事情。" class="headerlink" title="1) 如果你在写一个代码库/SDK，你想帮助开发者正确地使用它，Lint 规则就能派上用场。有了 Lint，你可以轻易地提醒他们忽略或做错的事情。"></a>1) 如果你在写一个代码库/SDK，你想帮助开发者正确地使用它，Lint 规则就能派上用场。有了 Lint，你可以轻易地提醒他们忽略或做错的事情。</h4><h4 id="2-如果你的团队有了新加入的开发者，Lint-可以帮助他快速了解团队的最佳实践，或命名惯例。"><a href="#2-如果你的团队有了新加入的开发者，Lint-可以帮助他快速了解团队的最佳实践，或命名惯例。" class="headerlink" title="2) 如果你的团队有了新加入的开发者，Lint 可以帮助他快速了解团队的最佳实践，或命名惯例。"></a>2) 如果你的团队有了新加入的开发者，Lint 可以帮助他快速了解团队的最佳实践，或命名惯例。</h4><h3 id="2-怎样自定义lint？"><a href="#2-怎样自定义lint？" class="headerlink" title="2.    怎样自定义lint？"></a>2.    怎样自定义lint？</h3><h4 id="Gradle"><a href="#Gradle" class="headerlink" title="Gradle:"></a>Gradle:</h4><p>自定义的 Lint 规则必须实现在一个新的模块中。以下是一个 build.gradle 例子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">apply plugin: <span class="string">'java'</span></span><br><span class="line">targetCompatibility = JavaVersion.VERSION_1_7</span><br><span class="line">sourceCompatibility = JavaVersion.VERSION_1_7</span><br><span class="line">configurations &#123;</span><br><span class="line">	lintChecks</span><br><span class="line">&#125;</span><br><span class="line">dependencies &#123;</span><br><span class="line">	compile <span class="string">'com.android.tools.lint:lint-api:24.3.1'</span></span><br><span class="line">	compile <span class="string">'com.android.tools.lint:lint-checks:24.3.1'</span></span><br><span class="line">	<span class="function">lintChecks <span class="title">files</span><span class="params">(jar)</span></span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function">jar </span>&#123;</span><br><span class="line">	manifest &#123;</span><br><span class="line">		attributes(<span class="string">'Lint-Registry'</span>: <span class="string">'com.captaintrain.android.lint.CaptainRegistry'</span>)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">defaultTasks <span class="string">'assemble'</span></span><br><span class="line"><span class="function">task <span class="title">install</span><span class="params">(type: Copy, dependsOn: build)</span> </span>&#123;</span><br><span class="line">from configurations.lintChecks</span><br><span class="line">into System.getProperty(<span class="string">'user.home'</span>) + <span class="string">'/.android/lint/'</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如你所见，为了实现自定义 Lint 规则，需要两个编译依赖关系。此外，还需要确切的 Lint-Registry，后文会介绍这是什么，现在只需记住这是强制要求。最后，创建一个小任务来快速安装新的 Lint 规则。</p>
<p>接着，使用../gradlew clean install编译并部署该模块。</p>
<p>配置好模块之后，让我们来看看如何编写第一条规则。</p>
<h4 id="规则一：Attr-（属性）必须有前缀"><a href="#规则一：Attr-（属性）必须有前缀" class="headerlink" title="规则一：Attr （属性）必须有前缀"></a>规则一：Attr （属性）必须有前缀</h4><p>在 CaptainTrain 项目中，我们都会在属性前面添加ct前缀，从而避免与其他代码库发生冲突。新的开发者很容易忘记这一点，因此写了如下规则：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AttrPrefixDetector</span> <span class="keyword">extends</span> <span class="title">ResourceXmlDetector</span> </span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Issue ISSUE = Issue.create(<span class="string">"AttrNotPrefixed"</span>,</span><br><span class="line">	<span class="string">"You must prefix your custom attr by `ct`"</span>,</span><br><span class="line">	<span class="string">"We prefix all our attrs to avoid clashes."</span>,</span><br><span class="line">	Category.TYPOGRAPHY,</span><br><span class="line">	<span class="number">5</span>,</span><br><span class="line">	Severity.WARNING,</span><br><span class="line">	<span class="keyword">new</span> Implementation(AttrPrefixDetector<span class="class">.<span class="keyword">class</span>,</span></span><br><span class="line"><span class="class">	<span class="title">Scope</span>.<span class="title">RESOURCE_FILE_SCOPE</span>))</span>;</span><br><span class="line">	<span class="comment">// Only XML files</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">appliesTo</span><span class="params">(@NonNull Context context,@NonNull File file)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> LintUtils.isXmlFile(file);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// Only values folder</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">appliesTo</span><span class="params">(ResourceFolderType folderType)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> ResourceFolderType.VALUES == folderType;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// Only attr tag</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Collection&lt;String&gt; <span class="title">getApplicableElements</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Collections.singletonList(TAG_ATTR);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// Only name attribute</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Collection&lt;String&gt; <span class="title">getApplicableAttributes</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Collections.singletonList(ATTR_NAME);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">visitElement</span><span class="params">(XmlContext context, Element element)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">final</span> Attr attributeNode = element.getAttributeNode(ATTR_NAME);</span><br><span class="line">		<span class="keyword">if</span> (attributeNode != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">final</span> String val = attributeNode.getValue();</span><br><span class="line">		<span class="keyword">if</span> (!val.startsWith(<span class="string">"android:"</span>) &amp;&amp; !val.startsWith(<span class="string">"ct"</span>)) &#123;</span><br><span class="line">			context.report(ISSUE,</span><br><span class="line">			attributeNode,</span><br><span class="line">			context.getLocation(attributeNode),</span><br><span class="line">			<span class="string">"You must prefix your custom attr by `ct`"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如你所见，我们继承了ResourceXmlDetector类。Detector 类允许我们发现问题，并报告Issue。首先，我们必须明确寻找什么：</p>
<ul>
<li><p>第一个appliesTo方法会只保留 XML 文件。</p>
</li>
<li><p>第二个appliesTo方法会只保留资源文件夹中的values。</p>
</li>
<li><p>getApplicableElements 方法会只保留attr XML 元素。</p>
</li>
<li><p>getApplicableAttributes 方法会只保留name XML 属性。</p>
</li>
</ul>
<p>过滤之后，我们使用简单的算法实现visitElement方法。一旦发现某个attr XML 标记的name属性不源自安卓也不以ct前缀，我们就报告一个Issue。该Issue按照如下方式声明在类的头部：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Issue ISSUE = Issue.create(<span class="string">"AttrNotPrefixed"</span>,</span><br><span class="line"><span class="string">"You must prefix your custom attr by `ct`"</span>,</span><br><span class="line"><span class="string">"To avoid clashes, we prefixed all our attrs."</span>,</span><br><span class="line">Category.TYPOGRAPHY,</span><br><span class="line"><span class="number">5</span>,</span><br><span class="line">Severity.WARNING,</span><br><span class="line"><span class="keyword">new</span> Implementation(AttrPrefixDetector<span class="class">.<span class="keyword">class</span>,</span></span><br><span class="line"><span class="class"><span class="title">Scope</span>.<span class="title">RESOURCE_FILE_SCOPE</span>))</span>;</span><br></pre></td></tr></table></figure>

<p>其中，每个参数都很重要，而且是强制性参数。</p>
<ul>
<li><p>AttrNotPrefixed 是 Lint 规则的 id，必须是唯一的。</p>
</li>
<li><p>You must prefix your custom attr by ct（必须以 ct 作为自定义属性的前缀）是简述。</p>
</li>
<li><p>To avoid clashes, we prefixed all our attrs.（为避免冲突，所有属性均添加前缀。）是更为详细的解释。</p>
</li>
<li><p>5是优先级系数。必须是1到10之间的某个值。</p>
</li>
<li><p>WARNING 是严重程度。此处我们只选择WARNING，这样即便存在该问题，代码也能安全运行。</p>
</li>
<li><p>Implementation是Detector间的桥梁，用于发现问题。Scope则用于分析问题。在本例中，我们必须处于资源文件层面才能分析前缀问题。</p>
</li>
</ul>
<p>你可能也发现了，其实所需的代码非常简单易懂。你只需小心所用的范围以及为Issue输入的值即可。</p>
<h4 id="规则二：生产环境下禁止-log"><a href="#规则二：生产环境下禁止-log" class="headerlink" title="规则二：生产环境下禁止 log"></a>规则二：生产环境下禁止 log</h4><p>在 CaptainTrain 应用中，我们将所有Log调用都包装到一个新的类里。由于在生产环境下，日志有可能妨碍应用性能与用户数据的安全，该类旨在BuildConfig.DEBUG为非时禁用日志。此外，该类还能帮助日志排版，以及提供一些其他特性。举例如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoggerUsageDetector</span> <span class="keyword">extends</span> <span class="title">Detector</span> <span class="keyword">implements</span> <span class="title">Detector</span>.<span class="title">ClassScanner</span> </span>&#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Issue ISSUE = Issue.create(<span class="string">"LogUtilsNotUsed"</span>,</span><br><span class="line">	<span class="string">"You must use our `LogUtils`"</span>,</span><br><span class="line">	<span class="string">"Logging should be avoided in production for security and performance reasons. Therefore, we created a LogUtils that wraps all our calls to Logger and disable them for release flavor."</span>,</span><br><span class="line">	Category.MESSAGES,</span><br><span class="line">	<span class="number">9</span>,</span><br><span class="line">	Severity.ERROR,</span><br><span class="line">	<span class="keyword">new</span> Implementation(LoggerUsageDetector<span class="class">.<span class="keyword">class</span>,</span></span><br><span class="line"><span class="class">	<span class="title">Scope</span>.<span class="title">CLASS_FILE_SCOPE</span>))</span>;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;String&gt; <span class="title">getApplicableCallNames</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Arrays.asList(<span class="string">"v"</span>, <span class="string">"d"</span>, <span class="string">"i"</span>, <span class="string">"w"</span>, <span class="string">"e"</span>, <span class="string">"wtf"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;String&gt; <span class="title">getApplicableMethodNames</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Arrays.asList(<span class="string">"v"</span>, <span class="string">"d"</span>, <span class="string">"i"</span>, <span class="string">"w"</span>, <span class="string">"e"</span>, <span class="string">"wtf"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">checkCall</span><span class="params">(@NonNull ClassContext context,</span></span></span><br><span class="line"><span class="function"><span class="params">	@NonNull ClassNode classNode,</span></span></span><br><span class="line"><span class="function"><span class="params">	@NonNull MethodNode method,</span></span></span><br><span class="line"><span class="function"><span class="params">	@NonNull MethodInsnNode call)</span> </span>&#123;</span><br><span class="line">		String owner = call.owner;</span><br><span class="line">		<span class="keyword">if</span> (owner.startsWith(<span class="string">"android/util/Log"</span>)) &#123;</span><br><span class="line">			context.report(ISSUE,</span><br><span class="line">			method,</span><br><span class="line">			call,</span><br><span class="line">			context.getLocation(call),</span><br><span class="line">			<span class="string">"You must use our `LogUtils`"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如你所见，规则二的模式与规则一相同。方法getApplicableCallNames与getApplicableMethodNames用于明确寻找的目标。之后，我们找出问题并创建之。唯一的不同在于，我们不再继承XmlResourceDetector类，而是仅继承Detector类，并实现ClassScanner接口以处理 Java 类检查。所以，实际上，规则二的变化没有很多。如果仔细查看XmlResourceDetector类，会发现它只是实现XmlScanner的Detector类。因此，所有规则都适用的总结如下：我们只需继承Detector并实现合适的Scanner接口即可。</p>
<p>最后，改变Issue的范围并关闭CLASS_FILE_SCOPE。此处，要想找到问题，只需分析一个 Java 类文件即可。有时，你需要分析多个 Java 类文件才能发现问题，所以你需要使用ALL_CLASS_FILES。范围的选择非常重要，因此请小心谨慎。点击此处可查看全部范围。</p>
<p>虽然问题描述可能不很清楚，但一个Detector可以发现多个问题。此外，通过一次运行就能处理所有问题，因此可以有效提高应用性能。</p>
<p>登记<br>此处，我们遗漏了一项重要的事情：登记！我们需要将新创建的问题登记到所有处理过的 lint 检查列表中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">CaptainRegistry</span> <span class="keyword">extends</span> <span class="title">IssueRegistry</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> List&lt;Issue&gt; <span class="title">getIssues</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> Arrays.asList(LoggerUsageDetector.ISSUE, AttrPrefixDetector.ISSUE);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>登记过程也非常简单。我们只需继承IssueRegistry类并实现getIssues方法，从而返回我们的自定义问题。该类必须与早前在build.gradle中声明的类保持一致。<br>结论<br>虽然只举出了两个简单的例子，但希望大家能知道：Lint 是非常强大的。只是你要编写适合自己的规则。</p>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2016/06/21/%E8%87%AA%E5%AE%9A%E4%B9%89Lint/">https://zhangpingv5.gitee.io/2016/06/21/%E8%87%AA%E5%AE%9A%E4%B9%89Lint/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/lint/"># lint</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2016/10/21/Intent%E9%9A%90%E5%BC%8F%E8%B0%83%E7%94%A8-%E5%90%AF%E5%8A%A8%E5%85%B6%E5%AE%83%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/">Intent隐式调用-启动其它应用程序</a>
            
            
            <a class="next" rel="next" href="/2016/06/20/Android%20Studio%E4%B8%8Alint%E7%9A%84%E4%BD%BF%E7%94%A8/">Android Studio上lint的使用</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
